package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduCourseRatingMapper;
import com.zenith.edu.po.domain.EduCourseRating;
import com.zenith.edu.po.dto.EduCourseRatingDTO;
import com.zenith.edu.po.vo.EduCourseRatingVO;
import com.zenith.edu.service.EduCourseRatingService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduCourseRatingServiceImpl extends ServiceImpl<EduCourseRatingMapper, EduCourseRating> implements EduCourseRatingService {
    
    @Override
    public EduCourseRatingVO createCourseRating(EduCourseRatingDTO dto) {
        EduCourseRating courseRating = new EduCourseRating();
        BeanUtils.copyProperties(dto, courseRating);
        this.save(courseRating);
        
        EduCourseRatingVO vo = new EduCourseRatingVO();
        BeanUtils.copyProperties(courseRating, vo);
        return vo;
    }
    
    @Override
    public EduCourseRatingVO getCourseRatingById(Long id) {
        EduCourseRating courseRating = this.getById(id);
        if (courseRating == null) {
            return null;
        }
        
        EduCourseRatingVO vo = new EduCourseRatingVO();
        BeanUtils.copyProperties(courseRating, vo);
        return vo;
    }
    
    @Override
    public EduCourseRatingVO updateCourseRating(EduCourseRatingDTO dto) {
        EduCourseRating courseRating = new EduCourseRating();
        BeanUtils.copyProperties(dto, courseRating);
        this.updateById(courseRating);
        
        EduCourseRatingVO vo = new EduCourseRatingVO();
        BeanUtils.copyProperties(courseRating, vo);
        return vo;
    }
    
    @Override
    public boolean deleteCourseRating(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduCourseRatingVO> getCourseRatingList() {
        List<EduCourseRating> courseRatingList = this.list();
        return courseRatingList.stream().map(courseRating -> {
            EduCourseRatingVO vo = new EduCourseRatingVO();
            BeanUtils.copyProperties(courseRating, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}